/*
 * This packet is the response from the server of the games requested by the 
 * client that are supported.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class GameListPacket extends Packet {
    
    short game_list_len;
    short[][] game_list;
    
    public GameListPacket( short[][] game_list ) throws PacketException {
        
        /*Set up the header*/
        message_type = MSG_GAME_LIST;
        game_name = 0;
        game_version = 0;
        
        /*game list*/
        this.game_list = game_list;
        
        game_list_len = (short)game_list.length;
        
        data_area_size = 4 + game_list_len*4;
    }
    
    public GameListPacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        
        /*grab the game list len and reserved 0 padding*/
        game_list_len = buf.getShort();
        short empty = buf.getShort();
        
        /*grab the game list*/
        game_list = new short[game_list_len][2];
        for ( int i = 0; i < game_list_len; i++ ) {
            game_list[i][0] = buf.getShort();
            game_list[i][1] = buf.getShort();
        }
    }
	
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        
        /*put in the game list len and reserved 0 padding*/
        buf.putShort( game_list_len );;
        buf.putShort( (short)0 );
        
        /*put in game list*/
        for ( int i = 0; i < game_list_len; i++ ) {
            buf.putShort( game_list[i][0] );
            buf.putShort( game_list[i][1] );
        }
        
        return buf.array();
    }
    
    
    public String toString() {
        String ret = super.toString() + "\ngame_list_len = " + game_list_len;
        ret = ret + "\ngame_list=";
        for ( int i = 0; i < game_list_len; i++ ) {
            ret = ret + "\n  game("+i+")="+game_list[i][0] + "," + game_list[i][1];
        }
        return ret;
    }
    
    public short get_game_list_len() { return game_list_len; }
    public short[][] get_game_list() { return game_list; }
    
}
